package org.jeecg.modules.cascv.image.service.impl;

import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.cascv.image.entity.GdqvXml;
import org.jeecg.modules.cascv.image.entity.GmvIds;
import org.jeecg.modules.cascv.image.mapper.GmvIdsMapper;
import org.jeecg.modules.cascv.image.service.IGmvIdsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @Description: ids导入表
 * @Author: jeecg-boot
 * @Date: 2022-08-31
 * @Version: V1.0
 */
@Service
public class GmvIdsServiceImpl extends ServiceImpl<GmvIdsMapper, GmvIds> implements IGmvIdsService {

    @Autowired
    private GdqvParaServiceImpl gdqvParaService;
    @Autowired
    private GdqvXmlServiceImpl gdqvXmlService;

    @Override
    public Result<?> importFile(HttpServletRequest request, HttpServletResponse response, Class<GmvIds> gmvIdsClass) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            // 获取上传文件对象
            MultipartFile file = entity.getValue();
            String filename = file.getOriginalFilename();
            if (StringUtils.isBlank(filename)) {
                return Result.ok("文件名不能为空！");
            }
            try {
                GmvIds gmvIds = new GmvIds();
                gmvIds.setFileraw(file.getBytes());
                int pos = filename.lastIndexOf('.');
                if (pos > -1) {
                    gmvIds.setIdsName(filename.substring(0, pos));
                } else {
                    gmvIds.setIdsName(filename);
                }
                gmvIds.setIdsType(request.getParameter("idsType"));
                UpdateWrapper<GmvIds> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("ids_name", gmvIds.getIdsName());
                this.saveOrUpdate(gmvIds, updateWrapper);
                //解析xml并保存到数据库
                gdqvParaService.gongchengXmlInsert(file, 2);
                gdqvXmlService.insert(file, 12, false);
                GdqvXml gmvXml = gdqvXmlService.getOne(new QueryWrapper<GdqvXml>()
                        .eq("del_flag", 0)
                        .eq("xml_name", gmvIds.getIdsName()), false);
                gmvXml.setApid(request.getParameter("apid"));
                gmvXml.setSsid(request.getParameter("ssid"));
                gmvXml.setSdty(request.getParameter("sdty"));
                if(gmvXml.getApid() != null){
                    gmvXml.setApid(gmvXml.getApid().toUpperCase());
                }
                if(gmvXml.getSsid() != null){
                    gmvXml.setSsid(gmvXml.getSsid().toUpperCase());
                }
                if(gmvXml.getSdty() != null){
                    gmvXml.setSdty(gmvXml.getSdty().toUpperCase());
                }
                gdqvXmlService.updateById(gmvXml);

                try {
                    String reloadRes = HttpUtil.get("http://127.0.0.1:9855/reload");
                    System.out.println("更新结果:" + reloadRes);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return Result.ok("文件导入成功！");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Result.error("文件导入失败！");
    }
}
